<?php

namespace mpend\modules\demand\models;

use Yii;
use common\helpers\ArrayHelper;
use common\models\demand\DemandAttach;

/**
 * 方案基础模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BaseDemandForm extends \common\base\Model {

    /**
     * @var string 
     */
    public $user_id;

    /**
     * @var string 随机字符串
     */
    public $content;

    /**
     * @var array 签名字符串
     */
    public $images;

    /**
     * @var array 签名字符串
     */
    public $attachs;

    /**
     * @var object 客户端
     */
    private $_files = [];

    /**
     * @var string 缩略图
     */
    private $_thumb = '';

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'content'], 'required'],
            [['user_id'], 'integer'],
            [['content'], 'string', 'max' => 1000],
            ['content', 'validateContent'],
            ['images', 'validateImages'],
            ['attachs', 'validateAttachs'],
        ];
    }

    /**
     * 校验请求的签名
     * @param string $attribute `sign`
     */
    public function validateContent($attribute) {
        if ($this->hasErrors() || !YII_ENV_PROD) {
            return;
        }
        $content = $this->$attribute;
        /* @var $wechat \common\components\wechat\MpWechat */
        $wechat = Yii::$app->wechatMp;
        if (!$wechat->checkMsgSec($content)) {
            return $this->addError($attribute, '所发布内容含违规信息');
        }
    }

    /**
     * 校验请求的签名
     * @param string $attribute `sign`
     */
    public function validateImages($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $images = $this->$attribute;
        if (empty($images)) {
            return;
        }
        $this->_thumb = current($images);
    }

    /**
     * 验证请求过期时间
     * @param string $attribute `build`
     */
    public function validateAttachs($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $attachs = $this->$attribute;
        $data = [];
        foreach ($attachs as $attach) {
            $path = '';
            $description = '需求附件文件';
            if (is_string($attach)) {
                $path = $attach;
            } elseif (is_array($attach)) {
                $path = ArrayHelper::getValue($attach, 'path');
                $description = ArrayHelper::getValue($attach, 'name', $description);
            } else {
                continue;
            }
            $data[] = [
                'format' => DemandAttach::FORMAT_FILE,
                'path' => $path,
                'description' => $description,
                'ext' => pathinfo($path, PATHINFO_EXTENSION),
            ];
        }
        $this->_files = $data;
    }

    /**
     * 获取APP授权应用客户端
     * @return array
     */
    protected function getAttachFiles() {
        return $this->_files;
    }

    /**
     * 获取APP授权应用客户端
     * @return array
     */
    protected function getThumb() {
        return $this->_thumb;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'content' => '内容',
            'user_id' => '用户',
            'images' => '图片',
            'attachs' => '文件',
        ];
    }

}
